package com.nhh.v.controller;

import com.google.common.collect.Maps;
import com.nhh.v.anotation.ValidateHeader;
import com.nhh.v.dto.RequiredRequestHeader;
import com.nhh.v.service.CoinsService;
import com.nhh.v.service.RecvNhhCoins;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ywp on 2018/9/3.
 */
@RestController
@RequestMapping(value = "/api/coins")
public class CoinController {

    @Autowired
    private CoinsService coinsService;

    @PostMapping(value = "qrCoinsFlow")
    public Mono<Map> qrCoinsFlow(
            @ValidateHeader @Valid RequiredRequestHeader requireHeader,
            @RequestParam(value = "psize", required = false, defaultValue = "20") String pSize,
            @RequestParam(value = "pindex", required = false, defaultValue = "0") String pIndex) {

        HashMap<String, Object> qrCoinsFlowMap = Maps.newHashMap();
        qrCoinsFlowMap.put("retCode", "0");
        qrCoinsFlowMap.put("retMsg", "成功");
        qrCoinsFlowMap.put("psize", pSize);
        qrCoinsFlowMap.put("rows", coinsService.qrCoinsFlow(requireHeader.getUid(), pIndex, pSize));
        if (pIndex.equals("0")) {
            qrCoinsFlowMap.put("coinsInfo", coinsService.getCoinsInfo(requireHeader.getUid()));
        }
        return Mono.just(qrCoinsFlowMap);
    }

    @PostMapping(value = "recvCoins")
    public Mono<Map> receiveCoins(
            @ValidateHeader @Valid RequiredRequestHeader requireHeader,
            @RequestParam(value = "coinIds") String coinIds) {

        HashMap<String, Object> qrCoinsFlowMap = Maps.newHashMap();
        //todo 调用存储过程 sp_recvNhhCoins
        RecvNhhCoins recvNhhCoins = coinsService.recvNhhCoins(coinIds);
        qrCoinsFlowMap.put("retCode", recvNhhCoins.retCode());
        qrCoinsFlowMap.put("retMsg", recvNhhCoins.retMsg());
        qrCoinsFlowMap.put("userNhhcoins", recvNhhCoins.userNhhCoins());
        return Mono.just(qrCoinsFlowMap);
    }

    @PostMapping(value = "qrMycoins")
    public Mono<Map> queryMyCoins(
            @ValidateHeader @Valid RequiredRequestHeader requireHeader,
            @RequestParam(value = "psize", required = false, defaultValue = "20") String pSize,
            @RequestParam(value = "pindex", required = false, defaultValue = "0") String pIndex) {

        HashMap<String, Object> qrMyCoinsMap = Maps.newHashMap();
        qrMyCoinsMap.put("retCode", "0");
        qrMyCoinsMap.put("retMsg", "成功");
        qrMyCoinsMap.put("rows", coinsService.qrMyCoins(requireHeader.getUid(), "wak"));
        return Mono.just(qrMyCoinsMap);
    }
}
